<template>
  <Spin :spinning="loading" tip="Loading...">
  <div>
    <!-- table -->
    <FormPanel ref="formPanel" :list="formList" :cols="2" :formType="'add'" labelWidth="110" :isSubmitBtn="false"
               @formChange="formChangeHandle"/>
    <BaseDialog :visible.sync="visible" :title="'VIN新增'" destroyOnClose
                :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <VinVehicle @close="chexingclose" @save="chexingsave"></VinVehicle>
    </BaseDialog>
    <div style="padding-top: 3rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
    </div>
  </div>
  </Spin>
</template>
<script>
  import { newSave,quryRules,editSave} from '@/api/claim/factory/QualityInformationManagement_OEM/QualityInformationRules/index';
  import VinVehicle from '@/pages/claimDemage/tools/VinVehicle.vue';
  import moment from "moment";
export default {
  props: ['rowData'],
   components: {
     VinVehicle
  },
  data() {
    return {
      loading: false,
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      flterValue: {}, //参数
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      saveData: null,
      activeName: '审核记录',
    };
  },
  async mounted() {
    this.loading = true;
    if (this.rowData) {
      this.type = 2;
      this.ruleId = this.rowData.ruleId;
      await this.getViewDetail(this.ruleId);
      console.log(this.rowData, '带进来的信息');
    }
    this.loading = false;
  },
  methods: {
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    // //选中
    // selectChange(rows) {
    //   this.selectedRows = rows;
    //   console.log(this.selectedRows);
    // },
    async getViewDetail(ruleId) {
      let res = await quryRules({ id: ruleId });
      let data = res.data;
      this.createdBy = data.createdBy;
      //临时属性
      this.partId = data.partId; //主损件id
      this.isDeleted = data.isDeleted;
      //为2个form赋值
      for (let i in data) {
        this.formList.map(y => {
          if (i == y.fieldName) {
            y.initialValue = data[i];
          }
          if ('beginEffectDate|endEffectDate' == y.fieldName) {
            y.initialValue =[data.beginEffectDate,data.endEffectDate];
          }
        });
      }

    },
    //保存
    async saveDialog() {
      if(this.type==2){
        this.$refs.formPanel.SUBMIT_FORM();
        if (this.saveData) {
          this.saveData.ruleType = 58021004;
          this.saveData.ruleId = this.rowData.ruleId;
          this.saveData.isDeleted = this.rowData.isDeleted;
          this.saveData.createdAt = this.rowData.createdAt;
          this.saveData.createdBy = this.createdBy;
          this.saveData.appId = this.rowData.appId;
          if(this.saveData.proDate){
            this.saveData.proDate = moment(this.saveData.proDate).format('YYYY-MM-DD HH:mm:ss');
          }
          if(this.saveData.beginEffectDate){
            this.saveData.beginEffectDate = moment(this.saveData.beginEffectDate).format('YYYY-MM-DD HH:mm:ss');
          }
          if(this.saveData.endEffectDate){
            this.saveData.endEffectDate = moment(this.saveData.endEffectDate).format('YYYY-MM-DD HH:mm:ss');
          }
          this.loading = true;
          let res = await editSave(this.saveData);
          if (res.resultCode === 200) {
            this.$emit('save');
            this.$notify.success({ title: '提示信息', message: '保存成功' });
          } else {
            this.loading = false;
          }
        }
      }else {
        this.$refs.formPanel.SUBMIT_FORM();
        if (this.saveData) {
          this.saveData.ruleType = 58021004;
          this.saveData.partId = this.partId;
          if(this.saveData.proDate){
            this.saveData.proDate = moment(this.saveData.proDate).format('YYYY-MM-DD HH:mm:ss');
          }
          if(this.saveData.beginEffectDate){
            this.saveData.beginEffectDate = moment(this.saveData.beginEffectDate).format('YYYY-MM-DD HH:mm:ss');
          }
          if(this.saveData.endEffectDate){
            this.saveData.endEffectDate = moment(this.saveData.endEffectDate).format('YYYY-MM-DD HH:mm:ss');
          }
          this.loading = true;
          let res = await newSave(this.saveData);
          if (res.resultCode === 200) {
            this.$emit('save');
            this.$notify.success({title: '提示信息', message: '保存成功'});
          } else {
            this.loading = false;
          }
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    chooseOrderNo() {
      this.visible = true;
    },
    chexingclose() {
      this.visible = false;
    },
    chexingsave(obj) {
      this.formList.find(x => x.fieldName == 'vin').initialValue = obj[0].vin;
      this.formList.find(x => x.fieldName == 'proDate').initialValue = obj[0].prodDate;
      this.visible = false;
    },
    add() {},
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '规则编码',
          fieldName: 'ruleCode',
          readonly : true,
          disabled: true, // 置灰
        },
        {
          type: 'INPUT',
          label: '规则描述',
          fieldName: 'ruleInfo',
          rules: [{ required: true, message: '请输入规则编码', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          initialValue: '',
          readonly: true,
          rules: [{ required: true, message: '请选择VIN码', trigger: 'change' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        {
          type: 'DATE',
          label: '生产日期',
          fieldName: 'proDate',
          disabled:true,
          rules: [{ required: true, message: '请选择生产日期', trigger: 'change' }],
        },
        {
          type: 'RANGE_DATE',
          label: '生效时间',
          fieldName: 'beginEffectDate|endEffectDate',
          style: { minWidth: '200px' },
          rules: [{ required: true, message: '请输入时间', trigger: 'change' }],
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'ruleStatus',
          itemList: [{ text: '禁用', value: 0 },{ text: '启用', value: 1 }],
          rules: [{ required: true, message: '请选择状态', trigger: 'change' }],
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remarks',
          placeholder: '备注',
          rows: 2,
          selfCols: 2, // 元素占据几行
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
